/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ronsicecream.pos.wh.transaction.distributiontomainkitchendetail;

import com.buanalintas.bynga.Action;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Logger;
import com.buanalintas.bynga.Page;
import com.buanalintas.bynga.Persistence;
import com.ronsicecream.pos.entities.PosTransactionOutRequestMaterialToWarehouse;
import com.ronsicecream.pos.entities.PosTransactionOutRequestMaterialToWhDetail;
import com.ronsicecream.pos.entities.PosTransactionOutRequestToMkDetail;
import com.ronsicecream.pos.entities.PosTransactionWhDistributionToMainkitchenDetail;
import com.ronsicecream.pos.entities.PosTransactionWhDistributionToOutletDetail;
import java.util.List;
import javax.persistence.EntityManager;

/**
 *
 * @author user
 */
public class DistributionDetail extends Page{
    private Field distributionDetailId;
    private Field distributionDetailQuantity;
    private Field distributionDetailPrice;
    private Field distributionDetailSent;
    private Field distributionDetailUnsent;
    private Field distributionDetailDescription;
//    private Field distributionDetailSatuanId;
    private Field distributionDetailItemId;
    private Field distributionDetailDistributionId;

    public DistributionDetail() {
        super();

        setTheClass(PosTransactionWhDistributionToOutletDetail.class);




        distributionDetailId = new Field();
        distributionDetailId.setName("distributionDetailId");
        distributionDetailId.setCaption("id");
        //distributionDetailId.setInfo("distribution detail id");
        distributionDetailId.setListProperties(" align=\"left\" ");
        distributionDetailId.setInputProperties(" size=\"20\"");
        getFields().add(distributionDetailId);

        distributionDetailQuantity = new Field();
        distributionDetailQuantity.setName("distributionDetailQuantity");
        distributionDetailQuantity.setCaption("quantity");
        //distributionDetailQuantity.setInfo("distribution detail quantity");
        distributionDetailQuantity.setListProperties(" align=\"left\" width=\"5%\" nowrap");
        distributionDetailQuantity.setInputProperties(" size=\"20\"");
        getFields().add(distributionDetailQuantity);
        
        distributionDetailPrice = new Field();
        distributionDetailPrice.setName("distributionDetailPrice");
        distributionDetailPrice.setCaption("price");
        //distributionDetailPrice.setInfo("distribution detail quantity");
        distributionDetailPrice.setListProperties(" align=\"left\" width=\"5%\" nowrap");
        distributionDetailPrice.setInputProperties(" size=\"20\"");
        getFields().add(distributionDetailPrice);
        
        distributionDetailSent = new Field();
        distributionDetailSent.setName("distributionDetailSent");
        distributionDetailSent.setCaption("sent");
        //distributionDetailSent.setInfo("distribution detail quantity");
        distributionDetailSent.setListProperties(" align=\"left\" width=\"5%\" nowrap");
        distributionDetailSent.setInputProperties(" size=\"20\"");
        getFields().add(distributionDetailSent);
        
        distributionDetailUnsent = new Field();
        distributionDetailUnsent.setName("distributionDetailUnsent");
        distributionDetailUnsent.setCaption("unsent");
        //distributionDetailUnSent.setInfo("distribution detail quantity");
        distributionDetailUnsent.setListProperties(" align=\"left\" width=\"5%\" nowrap");
        distributionDetailUnsent.setInputProperties(" size=\"20\"");
        getFields().add(distributionDetailUnsent);

        distributionDetailDescription = new Field();
        distributionDetailDescription.setName("distributionDetailDescription");
        distributionDetailDescription.setCaption("description");
        //distributionDetailDescription.setInfo("distribution detail description");
        distributionDetailDescription.setListProperties(" align=\"left\" width=\"30%\" nowrap");
        distributionDetailDescription.setInputProperties(" size=\"20\"");
        distributionDetailDescription.setInputProperties(" cols=\"50\" rows=\"5\"");
        distributionDetailDescription.setInputType(Field.INPUT_TEXT_AREA);
        distributionDetailDescription.setInputCase(Field.CASE_UPPER);
        getFields().add(distributionDetailDescription);

//        distributionDetailSatuanId = new Field();
//        distributionDetailSatuanId.setName("distributionDetailSatuanId");
//        distributionDetailSatuanId.setCaption("satuan");
//        //posMasterSatuan.setInfo("pos master satuan");
//        distributionDetailSatuanId.setListProperties(" align=\"left\" width=\"1%\" nowrap");
//        distributionDetailSatuanId.setInputProperties(" size=\"20\"");
//        getFields().add(distributionDetailSatuanId);

        distributionDetailItemId = new Field();
        distributionDetailItemId.setName("distributionDetailItemId");
        distributionDetailItemId.setCaption("item");
        //posMasterItem.setInfo("pos master item");
        distributionDetailItemId.setListProperties(" align=\"left\" width=\"15%\" nowrap");
        distributionDetailItemId.setInputProperties(" size=\"20\"");
         distributionDetailItemId.getSelectorColumns().add(new String[]{"itemName", " Nama", "width=\"40%\""});
        distributionDetailItemId.getSelectorColumns().add(new String[]{"itemCode", " Code", "width=\"10%\" nowrap"});
        distributionDetailItemId.getSelectorColumns().add(new String[]{"itemTypeId", " Type", "width=\"20%\""});
        distributionDetailItemId.getSelectorColumns().add(new String[]{"itemMinimum", " Minimum", "width=\"10%\""});
        distributionDetailItemId.getSelectorColumns().add(new String[]{"itemSatuanId", " Satuan", "width=\"10%\""});
        distributionDetailItemId.getSelectorColumns().add(new String[]{"itemRasaId", " Rasa", "width=\"10%\""});
        getFields().add(distributionDetailItemId);

        distributionDetailDistributionId = new Field();
        distributionDetailDistributionId.setName("distributionDetailDistributionId");
        distributionDetailDistributionId.setCaption("distribution");
        //posTransactionDistributionToMainKitchen.setInfo("pos transaction distribution to main kitchen");
        distributionDetailDistributionId.setListProperties(" align=\"left\" width=\"1%\" nowrap");
        distributionDetailDistributionId.setInputProperties(" size=\"20\"");
        getFields().add(distributionDetailDistributionId);


        fillFields();
        getActions().add(Action.TRANS_DETAIL_LIST);
        getActions().add(Action.TRANS_DETAIL_ADD);
        getActions().add(Action.TRANS_DETAIL_EDIT);

        getFieldsAdd().clear();
        getFieldsAdd().add(distributionDetailItemId);
        getFieldsAdd().add(distributionDetailQuantity);
        getFieldsAdd().add(distributionDetailPrice);
        getFieldsAdd().add(distributionDetailDescription);

        getFieldsEdit().clear();
        getFieldsEdit().add(distributionDetailItemId);
        getFieldsEdit().add(distributionDetailQuantity);
        getFieldsEdit().add(distributionDetailPrice);
        getFieldsEdit().add(distributionDetailDescription);

        getFieldsList().clear();
        getFieldsList().add(distributionDetailItemId);
        getFieldsList().add(distributionDetailQuantity);
        getFieldsList().add(distributionDetailPrice);
        getFieldsList().add(distributionDetailSent);
        getFieldsList().add(distributionDetailUnsent);
        getFieldsList().add(distributionDetailDescription);
    }
    
    @Override
    public void loadDetail(List datas, String id) {
        try {
            EntityManager em = getEntityManager();
            List<PosTransactionOutRequestMaterialToWhDetail> request = em.createQuery("SELECT t FROM PosTransactionOutRequestMaterialToWhDetail t "
                    + " WHERE t.requestDetailRequestId.requestId=" + id.toString())
                    .setHint(Persistence.REFRESH_HINT, true).getResultList();
            datas.clear();
            for (PosTransactionOutRequestMaterialToWhDetail pd : request) {
                PosTransactionWhDistributionToOutletDetail rd = new PosTransactionWhDistributionToOutletDetail();
                rd.setDistributionDetailItemId(pd.getRequestDetailItemId());
                rd.setDistributionDetailQuantity(pd.getRequestDetailQuantity());
                System.out.println("Quantity " + rd.getDistributionDetailQuantity());
                rd.setDistributionDetailPrice(pd.getRequestDetailPrice());
//                rd.setReceiptDetailSatuanId(pd.getPurchaseOrderDetailSatuanId());
                rd.setDistributionDetailDescription(pd.getRequestDetailDescription());
//                if ((rd.getReceiptDetailQuantity().intValue()) > 0) {
                    datas.add(rd);
//                }
            }
        } catch (Exception e) {
            Logger.getInstance().log(e);
            System.out.println("Errrorr " + e);
        }
    }
}
